package com.platform.web.provider;

import com.platform.common.base.Page;
import com.platform.web.mapper.RoleMapper;
import com.platform.web.mapper.RolePermissionMapper;
import com.platform.web.mapper.RoleUserMapper;
import com.platform.web.model.Permission;
import com.platform.web.model.Role;
import com.platform.web.model.RolePermisson;
import com.platform.web.model.RoleUser;
import com.platform.web.service.IRoleService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * 系统角色接口实现类
 * 
 * @author LiangMingHui
 *
 */
@Component("roleService")
public class RoleServiceImpl implements IRoleService {

	@Resource
	private RolePermissionMapper rolePermissionMapper;

	@Resource
	private RoleUserMapper roleUserMapper;

	@Resource
	private RoleMapper roleMapper;

	@Override
	public List<Permission> getPermissionsByUserId(String userId) throws Exception {
		return rolePermissionMapper.getPermissionsByUserId(userId);
	}

	@Override
	public int deleteByPrimaryKey(String id) throws Exception {
		int source = roleMapper.deleteByPrimaryKey(id);
		if (source <= 0) {
			throw new Exception("删除失败");
		}
		return source;
	}

	@Override
	public int insert(Role record) throws Exception {
		checkSameRole(record);
		String id = UUID.randomUUID().toString();
		record.setId(id);
		return roleMapper.insert(record);
	}

	@Override
	public int insertSelective(Role record) throws Exception {
		checkSameRole(record);
		String id = UUID.randomUUID().toString();
		record.setId(id);
		return roleMapper.insertSelective(record);
	}

	@Override
	public Role selectByPrimaryKey(String id) throws Exception {
		return roleMapper.selectByPrimaryKey(id);
	}

	@Override
	public Role selectByRoleCode(String roleCode) throws Exception {
		return roleMapper.selectByRoleCode(roleCode);
	}

	@Override
	public int updateByPrimaryKeySelective(Role record) throws Exception {
		checkSameRole(record);
		return roleMapper.updateByPrimaryKeySelective(record);
	}

	@Override
	public int updateByPrimaryKey(Role record) throws Exception {
		checkSameRole(record);
		return roleMapper.updateByPrimaryKey(record);
	}

	@Override
	public Page<Role> selectRoleByPage(Map<String, Object> map) throws Exception {
		Page<Role> page = new Page<Role>();
		page.setPageNo((Integer) map.get("pageNo"));
		page.setPageSize((Integer) map.get("pageSize"));
		map.put("page", page);
		List<Role> results = roleMapper.selectRoleByPage(map);
		page.setResults(results);
		return page;
	}

	@Override
	public Map<String, Object> selectRoleByPage(Map<String, Object> map, Page<Object> page) throws Exception {
		page.setPageNo(page.getPageNo() / page.getPageSize() + 1);
		map.put("page", page);
		List<Role> data = roleMapper.selectRoleByPage(map);
		Map<String, Object> params = new HashMap<String, Object>(2);
		params.put("data", data);
		params.put("recordsTotal", page.getTotalPage());// 总页数
		params.put("recordsFiltered", page.getTotalRecord());
		return params;
	}

	@Override
	public int deleteBatchRoleByIds(List<String> ids) throws Exception {
		int result = roleMapper.getPermissionRoleCount(ids);
		if (result > 0) {
			throw new Exception("删除角色失败，角色已经绑定了权限项，请先解除角色权限绑定");
		}

		result = roleUserMapper.getRoleUserCountByRole(ids);

		if (result > 0) {
			throw new Exception("删除角色失败，角色已经授权用户，请先解除角色与用户的关系");
		}

		int result1 = roleMapper.deleteBatchRoleByIds(ids);
		if (result1 <= 0) {
			throw new Exception("删除角色失败");
		}
		return result1;
	}

	@Override
	public List<Permission> getRolePermissionsList(String roleId) throws Exception {
		return rolePermissionMapper.getRolePermissionsList(roleId);
	}

	@Override
	public int insertBatchRolePermission(Role roles) throws Exception {
		roleMapper.updateByPrimaryKeySelective(roles);
		String roleId = roles.getId();
		List<RolePermisson> list = roles.getRolePermisson();
		int deleteResult = rolePermissionMapper.deletePermissionsByRole(roleId);
		if (!list.isEmpty() && list.size() > 0) {
			deleteResult = rolePermissionMapper.insertBatchRolePermission(list);
			if (deleteResult <= 0) {
				throw new Exception("角色权限添加失败");
			}
		}
		return deleteResult;
	}

	@Override
	public List<Role> selectAllRoleList() throws Exception {
		return roleMapper.selectAllRoleList();
	}

	@Override
	public List<Role> getUserRoleList(String userId) throws Exception {
		return roleUserMapper.getUserRoleList(userId);
	}

	@Override
	public int insertBatchRoleUser(List<RoleUser> roleUserList, String userId) throws Exception {
		// 根据用户删除角色
		int deleteResult = roleUserMapper.deleteRoleByUser(userId);
		if (!roleUserList.isEmpty() && roleUserList.size() >= 0) {
			deleteResult = roleUserMapper.insertBatchUserRole(roleUserList);
			if (deleteResult <= 0) {
				throw new Exception("角色授权失败");
			}
		}
		return deleteResult;
	}

	/**
	 * 判断是否在相同的角色
	 *
	 * @param record
	 */
	private void checkSameRole(Role record) throws Exception {
		int result = roleMapper.getRoleCount(record);
		if (result > 0) {
			throw new Exception("存在相同的角色代码或者角色名称");
		}
	}
}
